import dayjs from "dayjs";
import pkg from "exceljs";
const { Workbook } = pkg;

export async function writeToFile(templatePath, targetPath, data) {
  const wb = new Workbook();
  await wb.xlsx.readFile(templatePath);
  const ws = wb.worksheets[0];

  for (const row of data) {
    const newRow = ws.addRow(row, "i+");

    if (row[0] === "") {
      newRow.eachCell((cell) => (cell.style.font.bold = true));
    } else {
      newRow.eachCell((cell) => (cell.style.font.bold = false));
    }
    newRow.getCell(2).alignment = {
      horizontal: "left",
      vertical: "middle",
      wrapText: true,
    };
  }

  const timestamp = dayjs().format("YYYY年MM月DD日");
  ws.getCell("A2").value = `时间：${timestamp}`;

  await wb.xlsx.writeFile(targetPath);
}

export async function writeToLakeFile(templatePath, targetPath, data) {
  const wb = new Workbook();
  await wb.xlsx.readFile(templatePath);
  const ws = wb.worksheets[0];

  for (const row of data) {
    ws.addRow(row, "i+");
  }

  await wb.xlsx.writeFile(targetPath);
}
